package com.oocl.crm.repository.impl;

import java.util.ArrayList;
import java.util.List;

import com.oocl.crm.db.MyLinkedList;
import com.oocl.crm.db.MyLinkedListFactory;
import com.oocl.crm.db.Node;
import com.oocl.crm.pojo.Customer;
import com.oocl.crm.repository.CustomerDataProcess;

public class CustomerDataProcessImpl implements CustomerDataProcess{
	private MyLinkedList<Customer> db;
	Customer[] customers;
	List<Customer> lists;
	public CustomerDataProcessImpl(MyLinkedList<Customer> db) {
		super();
		this.db = MyLinkedListFactory.getInstance();
	}

	public void addCustomer(Customer c) {
		c.setId(db.getCount()+1);
		db.insert_tail(c);
	}

	public void updateCustomer(String subCmd) {
		String[] splitString = subCmd.split(" ");
		String id = splitString[0];
		String paramsString = splitString[1];
		String[] params = paramsString.split(",");
			
		Node<Customer> customerNode;
		for(int i = 0;i<db.getCount();i++){
			customerNode = db.findNode(i);
			if(customerNode.getData().getId() == Integer.parseInt(id)){
				for(int j=0;j<params.length;j++){
					if("name".equals(params[j].substring(0,params[j].indexOf(":")))){
						customerNode.getData().setName(params[j].substring(params[j].indexOf(":")+1));
					}else if("sex".equals(params[j].substring(0,params[j].indexOf(":")))){
						customerNode.getData().setSex((params[j].substring(params[j].indexOf(":")+1)));
					}else if("birthday".equals(params[j].substring(0,params[j].indexOf(":")))){
						customerNode.getData().setBirthday((params[j].substring(params[j].indexOf(":")+1)));
					}else if("address".equals(params[j].substring(0,params[j].indexOf(":")))){
						customerNode.getData().setAddress((params[j].substring(params[j].indexOf(":")+1)));
					}else if("call".equals(params[j].substring(0,params[j].indexOf(":")))){
						customerNode.getData().setCall((params[j].substring(params[j].indexOf(":")+1)));
					}
				}
				break;
			}
		}
	}

	public void deleteCustomer(int id) {
		Node<Customer> customerNode;
		for(int i = 0;i<db.getCount();i++){
			customerNode = db.findNode(i);
			if(customerNode.getData().getId() == id){
				db.delNode(customerNode);
				break;
			}
		}
	}

	public Customer[] find(String[] condition) {
		Customer customer;
		if(condition[0].equals("0")){
			customers = new Customer[db.getCount()];
			for(int i = 0;i<db.getCount();i++){
				customer = db.findData(i);
				customers[i] = customer;
			}
		}else if(condition[0].equals("1")){
			boolean flag = false;
			customers = new Customer[1];
			int paramId = Integer.parseInt(condition[1].substring(condition[1].indexOf(":")+1));
			for(int i=0;i < db.getCount();i++){
				customer = db.findData(i);
				if(customer.getId() == paramId){
					customers[0] = customer;
					flag = true;
					break;
				}
			}
			if(!flag){
				customers = null;
			}
		}
		else if(condition[0].equals("2")){
			lists = new ArrayList<Customer>();
			String[] params = condition[1].split(",");
			String[] values = new String[params.length];
			for(int i = 0;i<params.length;i++){
				values[i] = params[i].substring(params[i].indexOf(":")+1);
			}
			for(int i=0;i < db.getCount();i++){
				customer = db.findData(i);
				if(customer.getName().equalsIgnoreCase(values[0])&&customer.getSex().equalsIgnoreCase(values[1])){
					lists.add(customer);
				}
			}
			customers = new Customer[lists.size()];
			for(int i=0;i<lists.size();i++){
				customers[i] = lists.get(i);
			}
		}
		return customers;
	}

	public Customer[] sort(String filed) {
		customers = new Customer[db.getCount()];
		if(filed.equalsIgnoreCase("id")){
			customers = sortById();
		}else if(filed.equalsIgnoreCase("name")){
			customers = sortByName();
		}
		return customers;
	}
	private Customer[] sortById(){
		Customer[] beans = LinkedToArray();
		Customer temp = null;
		for(int i=0;i<beans.length-1;i++){
			for(int j=0;j<beans.length-1-i;j++){
				if(beans[j].getId() > beans[j+1].getId()){
					temp = beans[j];
					beans[j] = beans[j+1];
					beans[j+1] = temp;
				}
			}
		}
		return beans;
	}
	private Customer[] sortByName(){
		Customer[] beans = LinkedToArray();
		Customer temp = null;
		for(int i=0;i<beans.length-1;i++){
			for(int j=0;j<beans.length-1-i;j++){
				if(beans[j].getName().compareTo(beans[j+1].getName()) > 0){
					temp = beans[j];
					beans[j] = beans[j+1];
					beans[j+1] = temp;
				}
			}
		}
		return beans;
		
	}
	private Customer[] LinkedToArray(){
		Customer[] customers = new Customer[db.getCount()];
		Node<Customer> p = db.getHead();
		for(int i = 0;i<db.getCount();i++){
			customers[i] = p.getData();
			p = p.getNext();
		}
		return customers;
	}

}
